package dingtalk

import (
	"crypto/hmac"
	"crypto/sha256"
	"fmt"
	"github.com/gogf/gf/v2/encoding/gbase64"
	"github.com/gogf/gf/v2/encoding/gurl"
	"github.com/gogf/gf/v2/os/gtime"
)

var DingTalk dingTalk

type dingTalk struct {
}

func (dingTalk) Sign(uri, secret string) string {
	timestamp := gtime.Now().TimestampMilli()

	m := hmac.New(sha256.New, []byte(secret))
	data := fmt.Sprintf("%d\n%s", timestamp, secret)
	_, _ = m.Write([]byte(data))
	result := m.Sum(nil)
	s := gurl.Encode(string(gbase64.Encode(result)))

	uri = fmt.Sprintf("%s&timestamp=%d&sign=%s", uri, timestamp, s)
	return uri
}
